package mhys.View.Admin;
import mhys.Dao.ClassesDao;
import mhys.Dao.TeaDao;
import mhys.Entity.Classes;
import mhys.Entity.Teacher;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

public class TeacherPanel extends JPanel {
    DefaultTableModel model;
    JTable jTable;
    TeaDao teaDao = new TeaDao();
    ClassesDao classesDao = new ClassesDao();

    public TeacherPanel() {
        this.setBackground(Color.WHITE);
        this.setLayout(new BorderLayout(10, 10));
        JLabel jLabel = new JLabel("教师姓名:");
        jLabel.setBounds(10, 10, 120, 30);

        JPanel jPanel1 = new JPanel();
        jPanel1.setLayout(null);
        jPanel1.setPreferredSize(new Dimension(100, 60));

        JTextField jTextField = new JTextField();
        jTextField.setBounds(130, 12, 100, 30);

        JButton updateButton = new JButton("修改");
        updateButton.setBounds(240, 12, 60, 30);

        JButton delButton = new JButton("删除");
        delButton.setBounds(310, 12, 60, 30);

        jTextField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                String key = jTextField.getText();
                List<Teacher> teachers = teaDao.getTeaByName(key);
                if (teachers != null) {
                    initData(teachers);
                } else {
                    JOptionPane.showMessageDialog(null, "查无此人");
                }
            }
        });

        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String[] rowData = new String[jTable.getColumnCount()];
                for (int i = 0; i < jTable.getColumnCount(); i++) {
                    rowData[i] = jTable.getValueAt(jTable.getSelectedRow(), i).toString();
                }

                final JDialog jDialog = new JDialog(App.jFrame, "修改信息", true);
                jDialog.setSize(300, 360);
                jDialog.setLocationRelativeTo(App.jFrame);
                jDialog.setLayout(null);
                JLabel idJLabel = new JLabel("教师ID：");
                idJLabel.setBounds(10, 20, 80, 25);
                final JTextField idTextField = new JTextField(rowData[0]);
                idTextField.setBounds(90, 20, 150, 25);

                JLabel number = new JLabel("工号：");
                number.setBounds(10, 60, 80, 25);
                final JTextField numberTextField = new JTextField(rowData[1]);
                numberTextField.setBounds(90, 60, 150, 25);

                JLabel nameJLabel = new JLabel("教师姓名：");
                nameJLabel.setBounds(10, 100, 80, 25);
                final JTextField nameTextField = new JTextField(rowData[2]);
                nameTextField.setBounds(90, 100, 150, 25);

                JLabel sexJLabel = new JLabel("性别：");
                sexJLabel.setBounds(10, 140, 80, 25);
                String[] itemSex = {"男", "女"};
                final JComboBox<String> jComboBox = new JComboBox<String>(itemSex);
                jComboBox.setBounds(90, 140, 150, 25);
                if ("男".equals(rowData[3])) {
                    jComboBox.setSelectedIndex(0);
                } else {
                    jComboBox.setSelectedIndex(1);
                }

                JLabel classJLabel = new JLabel("班级：");
                classJLabel.setBounds(10, 180, 80, 25);


                final JComboBox<String> classBox = new JComboBox<String>();
                classBox.addItem(rowData[4]);
                List<Classes> classes = classesDao.getClasses();
                for (Classes classes1 : classes) {
                    classBox.addItem(classes1.getC_name());
                }
                classBox.setBounds(90, 180, 150, 25);

                JLabel phoneJLabel = new JLabel("手机号：");
                phoneJLabel.setBounds(10, 220, 80, 25);
                final JTextField phoneTextField = new JTextField(rowData[5]);
                phoneTextField.setBounds(90, 220, 150, 25);

                JButton okButton = new JButton("修改");
                okButton.setBounds(100, 260, 80, 25);

                okButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int index = jTable.getSelectedRow();
                        if (index != -1) {
                            Integer t_id = (Integer) jTable.getValueAt(index, 0);
                            String t_number = numberTextField.getText();
                            String t_name = nameTextField.getText();
                            String sex = (String) jComboBox.getSelectedItem();
                            String classes = (String) classBox.getSelectedItem();
                            String phone = phoneTextField.getText();
                            Teacher teacher = new Teacher(t_id, t_name, t_number, sex, classes, phone);
                            int updateTeacher = teaDao.updateTea(teacher);
                            System.out.println(updateTeacher);
                            if (updateTeacher == 1) {
                                List<Teacher> teachers = teaDao.getTeachers();
                                JOptionPane.showMessageDialog(null, "修改成功");
                                jDialog.dispose();
                                initData(teachers);
                                jTable.clearSelection();
                                numberTextField.setText("");
                                nameTextField.setText("");
                                jComboBox.setSelectedItem("男");
                                classBox.setSelectedItem("");
                                phoneTextField.setText("");
                            }
                        } else {
                            JOptionPane.showMessageDialog(null, "请选择需要修改的用户");
                        }
                    }
                });

                jDialog.add(classBox);
                jDialog.add(classJLabel);
                jDialog.add(idJLabel);
                jDialog.add(idTextField);
                jDialog.add(numberTextField);
                jDialog.add(nameJLabel);
                jDialog.add(nameTextField);
                jDialog.add(number);
                jDialog.add(sexJLabel);
                jDialog.add(jComboBox);
                jDialog.add(okButton);
                jDialog.add(phoneJLabel);
                jDialog.add(phoneTextField);
                jDialog.setVisible(true);
            }
        });

        delButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = jTable.getSelectedRow();
                if (index != -1) {
                    Object id = jTable.getValueAt(index, 0);
                    int message = JOptionPane.showConfirmDialog(null, "是否确定删除", "选择一个选项", JOptionPane.YES_NO_CANCEL_OPTION);
                    if (message == JOptionPane.YES_OPTION) {
                        int delOrder = teaDao.delTea((Integer) id);
                        if (delOrder == 1) {
                            List<Teacher> teachers = teaDao.getTeachers();
                            JOptionPane.showMessageDialog(null, "删除成功");
                            initData(teachers);
                        }
                    } else {
                        List<Teacher> teachers = teaDao.getTeachers();
                        initData(teachers);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "请选择删除的地址");
                }
            }
        });


        jPanel1.add(updateButton);
        jPanel1.add(delButton);
//        jPanel1.add(searchButton);
        jPanel1.add(jTextField);
        jPanel1.add(jLabel);


        jLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
        String[] header = {"教师ID", "工号", "教师姓名", "性别", "班级", "手机号"};
        model = new DefaultTableModel(header, 0){
          public boolean isCellEditable(int row,int column) {
              return false;
          }
        };
        jTable = new JTable(model);
        jTable.getTableHeader().setReorderingAllowed(false);
        jTable.setRowHeight(30);
        JScrollPane jScrollPane = new JScrollPane(jTable);
        List<Teacher> teacherss = teaDao.getTeachers();
        initData(teacherss);
        jScrollPane.getViewport().setBackground(Color.WHITE);
        this.add(jPanel1, BorderLayout.NORTH);
        this.add(jScrollPane, BorderLayout.CENTER);

    }

    public void initData(List<Teacher> teacherss) {
        model.getDataVector().clear();
        for (Teacher teacher : teacherss) {
            Object[] data = {teacher.getT_id(), teacher.getT_number(), teacher.getT_name(), teacher.getSex(), teacher.getClasses(), teacher.getPhone()};
            model.addRow(data);
        }
        jTable.clearSelection();
    }
}
